package neuron;

import java.util.List;

public class NeuronFilter {

	public static boolean enabled = false;
	
	private static int filterSegment(Segment seg, double threshold)
	{
		int deleted = 0;
		for (int i = 0; i < 3; i++) {
			Segment branch = seg.getBranch(i);
			if (branch == null) continue;
			
			if (branch.isTerminalSegment() && branch.length() < threshold) {
				seg.deleteBranch(i);
				deleted++;
			} else {
				deleted += filterSegment(branch, threshold);
			}
		}
		return deleted;
	}
	
	public static void filterShortTerminalSegments(List<Dendrite> dendrites, double thresholdLength)
	{
		for (Dendrite d : dendrites) {
			while(true) {
				int deleted = filterSegment(d.root, thresholdLength);
				if (deleted == 0) break;
			}
		}
	}
	
	public static void filterNeuron(Neuron n)
	{
		if (!enabled) return;
		filterShortTerminalSegments(n.basalDendrites(), 10);
		filterShortTerminalSegments(n.getApicalDendrite().getObliqueDendrites(), 10);
		filterShortTerminalSegments(n.getApicalDendrite().getTuftDendriteAsList(), 10);		
	}
}
